package com.seupyy.controller;

import com.seupyy.pojo.Result;
import com.seupyy.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件的内容保存到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，从而防止文件被覆盖
        String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("/Users/huangxianjin/Desktop/test/" + fileName));
        String aliyunUrl = AliOssUtil.uploadFile(fileName, file.getInputStream());
        return Result.success(aliyunUrl);
    }
}
